package com.nti56.springboot.utils;



import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.quartz.CronExpression;

import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

public class StringUtil
{
	public static boolean isEmpty(String str){
		return str == null || str.equals("null") || str.trim().length()==0;
	}
	public static String getNonNullStr(Object o){
		if(o == null){
			return "";
		}else{
			return String.valueOf(o);
		}
	}
	
	public static boolean equals(String str1, String str2){
		if(str1 == null && str2 == null)
			return true;
		if(!isEmpty(str1) && !isEmpty(str2) && str1.equals(str2))
		{
			return true;
		}
		return false;
	}
	
	// If string is less than aLength, pad on left.
	public static String lPad(String aString, int aLength, String aPadCharacter)
	{
		if (aString == null)
			return null;
		int len = aString.length();
		StringBuffer pad = new StringBuffer("");
		if (aLength > len)
		{
			for (int i = 1; i <= aLength - len; i++)
				pad.append(aPadCharacter);
			return pad.toString() + aString;
		}
		return aString;
	}
	/**
	 * 去除字符；
	 * @param vStr
	 * @return
	 */
	public static String removeSymbol(String vStr)
	{
		if(!isEmpty(vStr))
		{
			String regEx="[-`~!@#$%^&*()+=|{}':;',//[//].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";   
			Pattern   p   =   Pattern.compile(regEx);      
			Matcher   m   =   p.matcher(vStr);      
			return m.replaceAll("").trim(); 
		}
		return vStr;
	}
	
	public static String subString(String vStr, int start, int end)
	{
		if(!isEmpty(vStr) && vStr.length()>=end)
		{
			return vStr.substring(start, end);
		}
		return vStr;
	}
	
	public static List<String> toList(String str, String separator)
	{
		List<String> list = new ArrayList<String>();
		if(!isEmpty(str) && str.indexOf(separator)>-1)
		{
			String[] arr = str.split(separator);
			for(String item : arr)
			{
				if(!isEmpty(item))
				{
					list.add(item);
				}
			}
		}
		return list;
	}
	public static String unEncode(String data)
	{
		if (null == data || "".equals(data)) return data;
		data = data.replaceAll("&quot;", "\"");
		data = data.replaceAll("&lt;", "<");
		data = data.replaceAll("&gt;", ">");
		data = data.replaceAll("&#92;", "\\\\");
		data = data.replaceAll("&apos;", "'");
		data = data.replaceAll("&amp;", "&");
		data = data.replaceAll("%3c%2f", "</");
		return data;
	}

	/**
	 * Escapes HTML special characters from a String
	 * @param string to be escaped
	 * @return String which has been escaped
	 */
	public static String encode(String string)
	{
		if (string != null) {
			int len = string.length();
			StringBuffer sb = new StringBuffer(len);
			char c;

			for (int i = 0; i < len; i++)
			{
				c = string.charAt(i);

				// HTML Special Chars
				if (c == '"')
					sb.append("&quot;");
				else if (c == '&')
					sb.append("&amp;");
				else if (c == '<')
					sb.append("&lt;");
				else if (c == '>')
					sb.append("&gt;");
				else if (c == '\\')
					sb.append("&#92;");
					//				else if (c == '\n')
					// Handle Newline
					//					sb.append("&lt;br/&gt;");
				else
				{
					int ci = 0xffff & c;
					if (ci < 160)
						// nothing special only 7 Bit
						sb.append(c);
					else if (ci < 256 || ci == 8212 || ci== 8216 || ci == 8217 || ci == 8220 || ci == 8221 || ci == 8230)
					{
						// Not 7 Bit use the unicode system
						sb.append("&#");
						sb.append(Integer.valueOf(ci).toString());
						sb.append(';');
					}
				}
			}

			return sb.toString();
		}
		else
			return null;
	}
	
	/**
	 * 根据cron 表达式获取对应的执行时间
	 * @param cronExpression
	 * @return
	 * @throws java.text.ParseException
	 */
	public static Date getNextFireTime(String cronExpression)
			throws java.text.ParseException {
		if (StringUtil.isEmpty(cronExpression)) {
			return null;
		}
		try {
			CronExpression cron = new CronExpression(cronExpression);
			Date nextFireDate = cron.getNextValidTimeAfter(new Date(System
					.currentTimeMillis()));
			return nextFireDate;
		} catch (ParseException e) {
			return null;
		}
	}
	
	public static String getActivate(String action) {
		if(Constant.DC_DATA_ACTION_ENABLE.equalsIgnoreCase(action)){
			return "1";
		}
		if(Constant.DC_DATA_ACTION_DISABLE.equalsIgnoreCase(action)){
			return "0";
		}
		return action;
	}
	
	public static String getVal(String str) {
		if (null == str)
			return "";
		return str;
	}
	
	public static String getVal(Object object) {
		if (null == object)
			return "";
		return object.toString();
	}
	
	public static boolean isBlank(CharSequence cs) {
        int strLen;
        if(cs != null && (strLen = cs.length()) != 0) {
          for(int i = 0; i < strLen; ++i) {
        	  //看是否有空白字符
              if(!Character.isWhitespace(cs.charAt(i))) {
                  return false;
              }
          }
          return true;
        } else {
          return true;
        }
    }
	
	/**
	 * toInParam("1,2,3,4,5")
	   * 字符串转换成数据库in语句的形式
	 * @param str
	 * @return
	 */
	public static String toInParam(String str){
        if(str==null||"".equals(str)){
        	return "''";
        }
        return "'"+str.replaceAll(",", "','")+"'";
    }

}
